package com.hellowx.qydcdev.controller;

import com.hellowx.qydcdev.entity.Customer;
import com.hellowx.qydcdev.entity.LoginVO;
import com.hellowx.qydcdev.exception.CustomException;
import com.hellowx.qydcdev.exception.CustomExceptionType;
import com.hellowx.qydcdev.service.CustomerService;
import com.hellowx.qydcdev.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created on 2022/1/4.
 *
 * @author Hello WX
 */
@Api(tags = "顾客模块")
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private CustomerService customerService;

    @ApiOperation("顾客注册")
    @PostMapping("register")
    public ResponseEntity<String> register(@RequestBody LoginVO loginVO,
                                           HttpServletRequest request){

        boolean flag = customerService.register(loginVO,request);
        if (flag){
            return ResponseEntity.ok("注册成功!!");
        }
        throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"注册失败!!");
    }

    @ApiOperation("顾客登录")
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody LoginVO loginVO){
        String loginToken = customerService.login(loginVO);
      return ResponseEntity.ok(loginToken);
    }

    @ApiOperation("通过登录令牌获取用户信息")
    @GetMapping("/token")
    public ResponseEntity<Customer> getCustomerByToken(String token){
        Customer customer = (Customer) redisUtils.get(token);
        return ResponseEntity.ok(customer);
    }

    @ApiOperation("更新顾客信息")
    @PostMapping("/update")
    public ResponseEntity<String> updateCustomer(@RequestBody Customer customer,HttpServletRequest request){
        String msg = customerService.updateCustomer(customer,request);
        return ResponseEntity.ok(msg);
    }
}
